package cn.easier.activity.console.quartz;

import cn.easier.activity.console.repository.UserLoginRepository;
import cn.easier.activity.console.utils.DataSubmissionUtil;
import cn.easier.activity.web.entity.UserLogin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class DataSubMissionUserActiveTask {

    private static Logger logger = LoggerFactory.getLogger(DataSubMissionUserActiveTask.class);

    private final UserLoginRepository userLoginRepository;

    @Value("${metadata.configuration.static-resource-path}")
    public String realPath;

    @Value("${metadata.activity-platform.wechat-config.image-path}")
    public String wechatConfigImagePath;

    public DataSubMissionUserActiveTask(UserLoginRepository userLoginRepository) {
        this.userLoginRepository = userLoginRepository;
    }

    /**
     * 定时任务：每1天循环一次,在线公司数据上送之每日活跃用户
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void updateOrderStauts() throws Exception {
        logger.info("=============== 定时任务：每1天循环一次,在线公司数据上送之每日活跃用户 开始 ===============");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        String nameTime = new SimpleDateFormat("yyMMdd").format(cal.getTime());
        String str = realPath+wechatConfigImagePath + "CDBHYDWAL02009A" + nameTime + "1000000.000.txt";
        byte b1[] = {0x01}; //这个是分隔符  他们要求的
        byte b2[] = {0x0a}; //这个是 每行数据 结束必须要加上的 他们要求的
        String str1 = new String(b1);
        String str2 = new String(b2);

        List<UserLogin> byCreateDate = userLoginRepository.findByCreateDateLike("%" + yesterday + "%");

        FileWriter fileWriter = null;
        fileWriter = new FileWriter(str);//创建文本文件

        for (UserLogin userLogin : byCreateDate) {
            StringBuilder sb = new StringBuilder();
            String phoneNum = userLogin.getPhoneNum();
            String beginTime = userLogin.getCreateDate();
            sb.append(phoneNum).append(str1).append(phoneNum).append(str1).append(phoneNum).append(str1).
                   append(str1).append(str1).append(str1).append(str1).append(beginTime).append(str1).
                    append(str1).append(str1).append("1").append(str1).append("0").append(str1).append(str1).append(str2);
            logger.info("数据上送活跃用户,拼接参数经过处理后的=====>", sb.toString());
            fileWriter.write(sb.toString());
        }
        fileWriter.flush();
        fileWriter.close();
        /////
        String bz2Path = DataSubmissionUtil.compress(str);
        String md5FilePath = DataSubmissionUtil.generateMD5File(bz2Path);
        File zipFile = new File(bz2Path);
        File MD5File = new File(md5FilePath);
        try {
            DataSubmissionUtil.upload(zipFile, "2009","");
            DataSubmissionUtil.upload(MD5File, "2009","MD5File");
            logger.info("================= 定时任务：在线公司数据上送之每日活跃用户 结束 =================");
        } catch (Exception e) {
            e.printStackTrace();
            logger.warn("================= 定时任务：在线公司数据上送之每日活跃用户 异常 =================", e);
        }
    }


}
